package com.xianggu.mybatis.headlerImpl;

import com.xianggu.mybatis.utils.TypeHandler;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chengjy
 * @date 2025/3/12
 */
public class TypeHandlerUtils {

    private static Map<Class<?>, TypeHandler> typeHandlerMap = new HashMap<>();

    static {
        // 注册类型处理器
        typeHandlerMap.put(String.class, new StringTypeHandler());
        typeHandlerMap.put(Integer.class, new IntegerTypeHandler());
        typeHandlerMap.put(int.class, new IntegerTypeHandler());
        typeHandlerMap.put(Double.class, new DoubleTypeHandler());
        typeHandlerMap.put(BigDecimal.class, new BigDecimalTypeHandler());
        typeHandlerMap.put(Date.class, new DateTypeHandler());
        typeHandlerMap.put(java.util.Date.class, new DateUtilTypeHandler());
        typeHandlerMap.put(Map.class, new MapTypeHandler());
    }

    public static TypeHandler getTypeHandler(Class<?> type) {
        for (Class<?> key : typeHandlerMap.keySet()) {
            if (key.isAssignableFrom(type)) {
                return typeHandlerMap.get(key);
            }
        }
        return null;
    }
}
